package cn.springstudio.springui.template;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.springstudio.springui.databinding.LayoutGridBinding;

/**
 * 名称：<b>GridActivity</b>
 * <p>作者：Spring Studio</p>
 * <p>版本：1.0</p>
 * <p>日期：2022/02/25 15:54</p>
 * <p>说明：</p>
 */
public abstract class GridActivity extends AppCompatActivity implements GridAdapter.OnGridListener {

    GridAdapter adapter;
    LayoutGridBinding binding;
    List<String> data = new ArrayList<>();
    Map<String, Class<?>> classMap = new LinkedHashMap<>();

    public GridActivity() {
    }

    protected void addAction(@NonNull String name, @NonNull Class<?> cls) {
        data.add(name);
        classMap.put(name, cls);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (binding == null) {
            binding = LayoutGridBinding.inflate(getLayoutInflater());
        }
        if (adapter == null) {
            adapter = new GridAdapter(this, this);
        }
        binding.recyclerView.setAdapter(adapter);

        setContentView(binding.getRoot());
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public String getItem(int position) {
        return data.get(position);
    }

    @Override
    public void onItemClicked(int position) {
        startActivity(new Intent(this, classMap.get(getItem(position))));
    }
}
